﻿using KZ.Express.ENT;
using KZ.Express.Presenter;
using KZ.Express.Resource;
using KZ.Utilities;
using System;
using System.Data;

namespace KZ.Express.Setup.Partner
{
    public partial class CustomerDetailView : FormBase
    {
        #region Private Variables

        private new CustomerDetailPresenter _presenter;

        #endregion Private Variables

        #region Private Methods

        private void CustomerDetailView_Load(object sender, EventArgs e)
        {
            LoadPartnerGroup();
            LoadData();
        }

        private void txtSubfix_TextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtSubfix.Text))
                txtCustomerCode.Text = txtSequence.Text + "_" + txtSubfix.Text;
            else
                txtCustomerCode.Text = txtSequence.Text;
        }

        private void cmbPartnerGroupID_SelectedValueChanged(object sender, EventArgs e)
        {
            DataRowView row = cmbPartnerGroupID.SelectedItem as DataRowView;
            txtSubfix.Text = row == null ? String.Empty : Utility.StringParse(row["PartnerGroupName"]);
        }

        protected override void LoadData()
        {
            try
            {
                switch (base.Action)
                {
                    case ActionName.Insert:
                        _presenter.LoadNextSequence(Sequences.Customer);
                        break;

                    case ActionName.Update:
                        _presenter.LoadEntity();
                        break;
                }
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        private void LoadPartnerGroup()
        {
            try
            {
                Global.LoadComboBox(cmbPartnerGroupID, _presenter.LoadForCMB<PartnerGroupENT>(null));
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        #endregion Private Methods

        #region Constructors

        public CustomerDetailView()
        {
            InitializeComponent();
            base.ObjectID = (int)Objects.Customer;
            base._presenter = _presenter = new CustomerDetailPresenter(this);
        }

        #endregion Constructors

        #region Public Properties

        public override string DocumentNumber
        {
            set
            {
                txtSequence.Text = value;
                txtCustomerCode.Text = value;
            }
        }

        public override EntityBase Entity
        {
            get
            {
                CustomerENT customer = new CustomerENT();
                customer.PartnerID = base.EntityID;
                customer.PartnerCode = txtCustomerCode.Text;
                customer.PartnerName = txtCustomerName.Text;
                customer.PartnerGroupID = Utility.IntNParse(cmbPartnerGroupID.SelectedValue);
                customer.Address = txtAddress.Text;
                customer.Phone = txtPhone.Text;
                customer.Email = txtEmail.Text;
                customer.TaxCode = txtTaxCode.Text;
                customer.SalesRep = txtSalesRep.Text;
                customer.IsCustomer = true;

                return customer;
            }
            set
            {
                if (value == null)
                {
                    ClearControl();
                    LoadData();
                }
                else
                {
                    CustomerENT customer = value as CustomerENT;
                    cmbPartnerGroupID.SelectedValueChanged -= cmbPartnerGroupID_SelectedValueChanged;

                    string sequence = customer.PartnerCode.Substring(0, 3);
                    txtSequence.Text = sequence;
                    if (customer.PartnerCode.Length > 3)
                        txtSubfix.Text = customer.PartnerCode.Substring(4, customer.PartnerCode.Length - 4);

                    txtCustomerCode.Text = customer.PartnerCode;
                    txtCustomerName.Text = customer.PartnerName;
                    cmbPartnerGroupID.SelectedValue = customer.PartnerGroupID;
                    txtAddress.Text = customer.Address;
                    txtPhone.Text = customer.Phone;
                    txtEmail.Text = customer.Email;
                    txtTaxCode.Text = customer.TaxCode;
                    txtSalesRep.Text = customer.SalesRep;

                    cmbPartnerGroupID.SelectedValueChanged += cmbPartnerGroupID_SelectedValueChanged;
                }
            }
        }

        public DataTable PartnerGroups
        {
            set { cmbPartnerGroupID.DataSource = value; }
        }

        #endregion Public Properties
    }
}